var mainView;//主webview
var curTab;//被选中的tabitem
var tabItemUrl = [
	'page/message/index.html',
	'page/template/index.html',
	'page/story/index.html',
	'page/center/index.html'
];
var barItemWebview = {};

mui.plusReady(function() {
	mainView = plus.webview.currentWebview();
	//初始化预加载所有页面
	inittabitemWebviews();
	//为每个按钮都添加点击事件
    addEventForTabitem();
    //关闭启动页面
    closeStartScreent();
});

function inittabitemWebviews() {
	for (var i = 0; i < tabItemUrl.length; i++) {
		if(!plus.webview.getWebviewById(barItemWebview[i])){
			barItemWebview[i] = mui.preload({
				url: tabItemUrl[i],
				id: tabItemUrl[i],
				styles: {
                        top: '0px',
                        bottom: '50px',
                        bounce: 'vertical',
                        bounceBackground: '#DCDCDC'
                    },
                waiting: {
                    autoShow: false
                }
			});
			barItemWebview[i].hide();
			mainView.append(barItemWebview[i]);
		}
	}
	barItemWebview[0].show();
}


function addEventForTabitem() {
	mui('.mui-bar-tab').on('tap','.mui-tab-item',function() {
		var tabItem = this;
		if(tabItem == curTab) {
			return;
		}
		curTab = tabItem;
		showCurWebview(tabItem.getAttribute('id'));
	});
}

// 展示内容
function showCurWebview(href) {
		var index = 0;
		for(var i = 0; i < tabItemUrl.length; i++) {
			if(tabItemUrl[i].indexOf(href) != -1) {
				index = i;
			} else {
				barItemWebview[i].hide();
			}
		}
		barItemWebview[index].show();
	}

function closeStartScreent() {
    plus.navigator.closeSplashscreen();
}